﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Administration.Claims;
using Microsoft.SharePoint.Utilities;
using System.Web.Security;

namespace AIS.SharePoint.AspNetFBA.Layouts.AIS.SharePoint.AspNetFBA
{
    public partial class ChangePassword : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Code.Claims ClaimHandler = new Code.Claims();
                MembershipUser User = null;
                try
                {
                    SPClaim UserClaim = SPClaimProviderManager.Local.DecodeClaim(Web.CurrentUser.LoginName);
                    User = ClaimHandler.MembeshipProvider.GetUser(UserClaim.Value, false);
                }
                catch { }

                if (User == null)
                {
                    lblStatus.Text = Code.Common.GetLocalizedResource("Resources:Page_ChangePassword_NotAvailableForADUsers");
                    btnOk.Enabled = false;
                    txtCurrentPassword.Enabled = false;
                    txtNewPassword.Enabled = false;
                    txtNewPasswordConfirm.Enabled = false;
                }
                else
                {
                    txtCurrentPassword.Focus();
                }

            }
            catch (Exception ex)
            {
                Code.Log.Error(ex, Microsoft.SharePoint.Administration.TraceSeverity.High, Code.Log.Category.Processing);
                SPUtility.TransferToErrorPage(ex.Message);
            }
        }

        public void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                Page.Validate();
                if (!Page.IsValid) { return; }

                Code.Claims ClaimHandler = new Code.Claims();
                MembershipUser User = null;
                try
                {
                    SPClaim UserClaim = SPClaimProviderManager.Local.DecodeClaim(Web.CurrentUser.LoginName);
                    User = ClaimHandler.MembeshipProvider.GetUser(UserClaim.Value, false);
                }
                catch { }

                if (User != null)
                {
                    if (!User.ChangePassword(txtCurrentPassword.Text, txtNewPassword.Text))
                    {
                        lblStatus.Text = Code.Common.GetLocalizedResource("$Resources:Page_ChangePassword_FailedValidatePassword");
                    }
                    else
                    {
                        lblStatus.Text = Code.Common.GetLocalizedResource("$Resources:Page_ChangePassword_Success") ;
                        txtCurrentPassword.Enabled = false;
                        txtNewPassword.Enabled = false;
                        txtNewPasswordConfirm.Enabled = false;
                        btnOk.Enabled = false;
                    }
                }
                else
                {
                    lblStatus.Text = Code.Common.GetLocalizedResource("$Resources:Page_ChangePassword_Unavailable");
                }
            }
            catch (Exception ex)
            {
                Code.Log.Error(ex, Microsoft.SharePoint.Administration.TraceSeverity.High, Code.Log.Category.Processing);
                lblStatus.Text = Code.Common.GetLocalizedResource("$Resources:Page_ChangePassword_Error");
            }
        }
    }
}
